package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.dao.StatisticMapper;
import net.zigin.nhi.could.waste.dao.SysUserHospitalMapper;
import net.zigin.nhi.could.waste.service.StatisticService;
import net.zigin.nhi.could.waste.vo.StatisticVo;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.vo.WarnBaseVo;
import net.zigin.nhi.could.waste.vo.WasteCountVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class StatisticServiceImpl implements StatisticService {
    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;

    @Override
    public Integer hospitalBaseCount() {
        return statisticMapper.hospitalBaseCount();
    }

    @Override
    public Map<String, Map<String, Object>> statistic() {
        // 获取医院id
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        // 获取日期
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM");
        String day = df.format(now);
        String month = df2.format(now);
        Map<String, Map<String, Object>> result = new HashMap<>();
        Map<String, Object> allMap = new HashMap<>();
        Map<String, Object> statusMap = initStatusMap();
        Map<String, Object> warnTypeMap = new HashMap<>();
        Map<String, Object> warnHospitalMap = new HashMap<>();
        Map<String, Object> warnDepartMap = new HashMap<>();
        Map<String, Object> classifyMap = initMap();
        // 数量 今日医废 医废类型 今日预警 预警医院/预警科室
        Integer hospitalCount = statisticMapper.hospitalBaseCount();
        Float allCollectWeight = statisticMapper.allCollectWeightToday(day, hospitalBaseId);
        Integer allWarnCount = statisticMapper.warnBaseCountToday(day, hospitalBaseId);
        List<WasteCountVo> status = statisticMapper.wasteCountStatusToday(day, hospitalBaseId);
        List<WasteCountVo> classify = statisticMapper.wasteClassifyWeightRatio(hospitalBaseId);
        List<WasteCountVo> warnType = statisticMapper.warnBaseTypeToday(day, hospitalBaseId);
        allMap.put("hospitalCount", hospitalCount);
        allMap.put("allCollectWeight", allCollectWeight);
        allMap.put("allWarnCount", allWarnCount);
        result.put("all", allMap);
        status.forEach(wasteCountVo -> statusMap.put(wasteCountVo.getStatus(), wasteCountVo.getWeight()));
        result.put("status", statusMap);
        classify.forEach(wasteCountVo -> classifyMap.put(wasteCountVo.getClassifyName(), wasteCountVo.getWeight()));
        result.put("classify", classifyMap);
        handleWarnType(warnTypeMap, warnType);
        result.put("warnType", warnTypeMap);
        if (StringUtil.isNullOrEmpty(hospitalBaseId)) {
            List<WasteCountVo> warnHospital = statisticMapper.warnBaseTopHospital(month);
            warnHospital.forEach(wasteCountVo -> warnHospitalMap.put(wasteCountVo.getHospitalName(), wasteCountVo.getCount()));
            result.put("warnHospital", warnHospitalMap);
        } else {
            List<WasteCountVo> warnDepart = statisticMapper.warnBaseTopDepart(month, hospitalBaseId);
            warnDepart.forEach(wasteCountVo -> warnDepartMap.put(wasteCountVo.getDepartName(), wasteCountVo.getCount()));
            result.put("warnDepart", warnDepartMap);
        }
        return result;
    }

    @Override
    public List<WarnBaseVo> warnList() {
        // 获取医院id
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        return statisticMapper.warnBaseOnLine(hospitalBaseId);
    }

    @Override
    public Map<String, Object> topHospitalOrDepart(Integer model) {
        // 获取医院id
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        // 获取日期
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM");
        String day = df.format(now);
        String month = df2.format(now);
        Map<String, Object> result = new HashMap<>();
        if (StringUtil.isNullOrEmpty(hospitalBaseId)) {
            List<WasteCountVo> wasteCountVos = statisticMapper.wasteCollectTopHospital(model == 1 ? day : month);
            wasteCountVos.forEach(wasteCountVo -> result.put(wasteCountVo.getHospitalName(), wasteCountVo.getWeight()));
        } else {
            List<WasteCountVo> wasteCountVos = statisticMapper.wasteCollectTopDepart(model == 1 ? day : month, hospitalBaseId);
            wasteCountVos.forEach(wasteCountVo -> result.put(wasteCountVo.getDepartName(), wasteCountVo.getWeight()));
        }
        return result;
    }

    @Override
    public Map<String, Map<String, Object>> hospitalStatisticInfo(String hospitalBaseId) {
        // 获取日期
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat df3 = new SimpleDateFormat("yyyy");
        String day = df.format(now);
        String month = df2.format(now);
        String year = df3.format(now);
        Calendar cld = Calendar.getInstance(Locale.CHINA);
        cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
        cld.setTimeInMillis(System.currentTimeMillis());//当前时间
        cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//周一
        String week = df.format(cld.getTime());
        WasteCountVo monthCollect = statisticMapper.wasteCollectInfo(month, hospitalBaseId);
        WasteCountVo weekCollect = statisticMapper.wasteCollectInfo(week, hospitalBaseId);
        List<WasteCountVo> dayCollect = statisticMapper.wasteCountStatusToday(day, hospitalBaseId);
        List<WasteCountVo> trendCollect = statisticMapper.wasteCollectTrendMonth(year, hospitalBaseId);
        Map<String, Object> monthMap = new HashMap<>();
        monthMap.put("count", monthCollect.getCount());
        monthMap.put("weight", monthCollect.getWeight());
        Map<String, Object> weekMap = new HashMap<>();
        weekMap.put("count", weekCollect.getCount());
        weekMap.put("weight", weekCollect.getWeight());
        Map<String, Object> dayCollectMap = new HashMap<>();
        dayCollect.forEach(wasteCountVo -> dayCollectMap.put(wasteCountVo.getStatus(), wasteCountVo.getWeight()));
        Map<String, Object> trendCollectMap = new HashMap<>();
        trendCollect.forEach(wasteCountVo -> trendCollectMap.put(wasteCountVo.getMonth(), wasteCountVo.getWeight()));
        Map<String, Map<String, Object>> result = new HashMap<>();
        result.put("month", monthMap);
        result.put("week", weekMap);
        result.put("day", dayCollectMap);
        result.put("tread", trendCollectMap);
        return result;
    }

    @Override
    public Map<String, Object> wasteCollectTrend(Integer model) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        String year = df.format(now);
        List<WasteCountVo> wasteCountVos;
        Map<String, Object> result = new HashMap<>();
        if (model == 1) {
            wasteCountVos = statisticMapper.wasteCollectTrendYear(hospitalBaseId);
            wasteCountVos.forEach(wasteCountVo -> result.put(wasteCountVo.getYear(), wasteCountVo.getWeight()));
        } else {
            wasteCountVos = statisticMapper.wasteCollectTrendMonth(year, hospitalBaseId);
            wasteCountVos.forEach(wasteCountVo -> result.put(wasteCountVo.getMonth(), wasteCountVo.getWeight()));
        }
        return result;
    }

    private Map<String, Object> initMap() {
        Map<String, Object> result = new HashMap<>();
        result.put("化学性", 0);
        result.put("药物性", 0);
        result.put("感染性", 0);
        result.put("损伤性", 0);
        result.put("病理性", 0);
        return result;
    }

    private Map<String, Object> initStatusMap() {
        Map<String, Object> result = new HashMap<>();
        result.put("in_depot", 0);
        result.put("out_depot", 0);
        result.put("do_collect", 0);
        return result;
    }

    private void handleWarnType(Map<String, Object> result, List<WasteCountVo> wasteCountVos) {
        int collectTime = 0, inTime = 0, outTime = 0, inWeight = 0, OutWeight = 0;
        for (WasteCountVo wasteCountVo : wasteCountVos) {
            String warnType = wasteCountVo.getWarnType();
            if (warnType.contains("do_collect_time")) {
                collectTime += wasteCountVo.getCount();
            } else if (warnType.contains("in_depot_time")) {
                inTime += wasteCountVo.getCount();
            } else if (warnType.contains("out_depot_time")) {
                outTime += wasteCountVo.getCount();
            } else if (warnType.contains("in_depot_weight")) {
                inWeight += wasteCountVo.getCount();
            } else if (warnType.contains("out_depot_weight")) {
                OutWeight += wasteCountVo.getCount();
            }
        }
        result.put("收集超时", collectTime);
        result.put("入库超时", inTime);
        result.put("出库超时", outTime);
        result.put("入库超重", inWeight);
        result.put("出库超重", OutWeight);
    }


}
